//
//  WVShapesView.m
//  Windows and Views
//
//  Created by Tim Isted on 07/10/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "WVShapesView.h"


@implementation WVShapesView

- (BOOL)acceptsFirstResponder {
    return YES;
}

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)calculateRedRect:(NSRect *)redRect blueRect:(NSRect *)blueRect {
    NSRect viewBounds = [self bounds];
    CGFloat halfWidth = (viewBounds.size.width / 2);
    NSDivideRect(viewBounds, redRect, blueRect, halfWidth, NSMinXEdge);
    *redRect = NSInsetRect(*redRect, 5, 5);
    *blueRect = NSInsetRect(*blueRect, 5, 5);
}

- (void)drawRect:(NSRect)dirtyRect {
    NSRect viewBounds = [self bounds];
    NSColor *currentColor = [NSColor whiteColor];
    [currentColor set];
    NSRectFill(viewBounds);
    
    currentColor = [NSColor blackColor];
    [currentColor set];
    NSFrameRect(viewBounds);
    
    NSRect redRect = NSZeroRect;
    NSRect blueRect = NSZeroRect;
    [self calculateRedRect:&redRect blueRect:&blueRect];
    
    if( shouldHighlightRedRect )
        currentColor = [NSColor darkGrayColor];
    else
        currentColor = [NSColor redColor];
    [currentColor set];
    NSRectFill(redRect);
    
    if( shouldHighlightBlueRect )
        currentColor = [NSColor darkGrayColor];
    else
        currentColor = [NSColor blueColor];
    [currentColor set];
    NSRectFill(blueRect);
}

- (void)mouseDown:(NSEvent *)theEvent {
    mouseDownPoint = [theEvent locationInWindow];
    mouseDownPoint = [self convertPoint:mouseDownPoint fromView:nil];
    
    NSRect redRect = NSZeroRect;
    NSRect blueRect = NSZeroRect;
    [self calculateRedRect:&redRect blueRect:&blueRect];
    
    if( NSPointInRect(mouseDownPoint, redRect) )
        shouldHighlightRedRect = YES;
    else if( NSPointInRect(mouseDownPoint, blueRect) )
        shouldHighlightBlueRect = YES;
    
    [self setNeedsDisplay:YES];
}

- (void)mouseUp:(NSEvent *)theEvent {
    NSPoint mouseLocation = [theEvent locationInWindow];
    mouseLocation = [self convertPoint:mouseLocation fromView:nil];
    
    NSRect redRect = NSZeroRect;
    NSRect blueRect = NSZeroRect;
    [self calculateRedRect:&redRect blueRect:&blueRect];
    
    if( NSPointInRect(mouseDownPoint, redRect) && NSPointInRect(mouseLocation, redRect) )
        NSLog(@"User clicked red rect");
    else if( NSPointInRect(mouseDownPoint, blueRect) && NSPointInRect(mouseLocation, blueRect) )
        NSLog(@"User clicked blue rect");
    
    shouldHighlightRedRect = NO;
    shouldHighlightBlueRect = NO;
    
    [self setNeedsDisplay:YES];
}

- (void)mouseDragged:(NSEvent *)theEvent {
    NSPoint mouseLocation = [theEvent locationInWindow];
    mouseLocation = [self convertPoint:mouseLocation fromView:nil];
    
    NSRect redRect = NSZeroRect;
    NSRect blueRect = NSZeroRect;
    [self calculateRedRect:&redRect blueRect:&blueRect];
    
    shouldHighlightRedRect = NO;
    shouldHighlightBlueRect = NO;
    
    if( NSPointInRect(mouseDownPoint, redRect) && NSPointInRect(mouseLocation, redRect) )
        shouldHighlightRedRect = YES;
    else if( NSPointInRect(mouseDownPoint, blueRect) && NSPointInRect(mouseLocation, blueRect) )
        shouldHighlightBlueRect = YES;
    
    [self setNeedsDisplay:YES];
}

@end
